<?php

/**
 * Operaciones filter form.
 *
 * @package    tercermilenio
 * @subpackage filter
 * @author     Your name here
 */
class OperacionesFormFilter extends BaseOperacionesFormFilter {

    public function configure() {

    }

    public function setup() {
        parent::setup();

        $opVistas = array(
            '' => 'Seleccione una vista',
            1 => 'Hoy',
            2 => 'Última semana',
            3 => 'Último mes',
        );

        $this->setWidgets(array(
            //'op_monto'   => new sfWidgetFormFilterInput(),
            //'op_motivo'  => new sfWidgetFormFilterInput(),
            'op_tipo' => new sfWidgetFormPropelChoice(array('model' => 'TipoOperaciones', 'add_empty' => 'Seleccione un tipo')),
            'op_usuario' => new sfWidgetFormPropelChoice(array('model' => 'sfGuardUser', 'add_empty' => 'Seleccione un usuario')),
            'created_at' => new sfWidgetFormFilterDate(array('from_date' => new sfWidgetFormJQueryDate(array('format' => '%day%/%month%/%year%')), 'to_date' => new sfWidgetFormJQueryDate(array('format' => '%day%/%month%/%year%')), 'with_empty' => false)),
            'operaciones_del_dia' => new sfWidgetFormInputCheckbox(),
        ));

        $this->setValidators(array(
            //'op_monto'   => new sfValidatorSchemaFilter('text', new sfValidatorInteger(array('required' => false))),
            //'op_motivo'  => new sfValidatorPass(array('required' => false)),
            'op_tipo' => new sfValidatorPropelChoice((array('required' => false, 'model' => 'TipoOperaciones', 'column' => 'tip_op_tipo'))),
            'op_usuario' => new sfValidatorPropelChoice(array('required' => false, 'model' => 'sfGuardUser', 'column' => 'id')),
            'created_at' => new sfValidatorDateRange(array('required' => false, 'from_date' => new sfValidatorDate(array('required' => false)), 'to_date' => new sfValidatorDate(array('required' => false)))),
            'operaciones_del_dia' => new sfValidatorBoolean(array('required' => false)),
        ));

        $this->widgetSchema->setNameFormat('operaciones_filters[%s]');

        $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);
    }

    public function addCreatedAtColumnCriteria(Criteria $criteria, $field, $values) {


        if (!empty($values)) {
            $inicioDia = '00:00:00';
            $finDia = '23:59:59';


            $filterInicio = $values['from'] . ' ' . $inicioDia;
            $filterFin = $values['to'] . ' ' . $finDia;


            $criteria->add(OperacionesPeer::CREATED_AT, $filterInicio, Criteria::GREATER_EQUAL);
            $criteria->addAnd(OperacionesPeer::CREATED_AT, $filterFin, Criteria::LESS_EQUAL);
        }
    }

    public function addOperacionesDelDiaColumnCriteria(Criteria $criteria, $field, $values) {

        if (!empty($values)) {
            $today = date('Y-m-d');

            $criteria->add(OperacionesPeer::CREATED_AT, $today . '%', Criteria::LIKE);
        }
    }


  public function getFields()
  {
    return array(
      'op_id'      => 'Number',
      'op_monto'   => 'Number',
      'op_motivo'  => 'Text',
      'op_tipo'    => 'ForeignKey',
      'op_usuario' => 'ForeignKey',
      'created_at' => 'Date',
      'operaciones_del_dia' => 'Checkbox',
    );
  }

}
